package com.punai.dataanalysis.srlib.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.entity.SysUser;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.DateUtil;
import com.punai.common.utils.poi.ExcelUtil;
import com.punai.dataanalysis.srlib.bo.GbInventoryPriceBo;
import com.punai.dataanalysis.srlib.bo.InventoryPriceBo;
import com.punai.dataanalysis.srlib.service.GbInventoryPriceService;
import com.punai.dataanalysis.srlib.vo.GbInventoryPriceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/dataanalysis/srlib/gbInventory")
public class GbInventoryPriceController extends BaseController {

    @Autowired
    private GbInventoryPriceService gbInventoryPriceService;

    @PreAuthorize("@ss.hasPermi('srlib:gbinventory:list')")
    @GetMapping("/priceList")
    public TableDataInfo priceList(GbInventoryPriceVo vo) {
        return gbInventoryPriceService.selectConfigInventoryLibList(vo);
    }

    @PreAuthorize("@ss.hasPermi('srlib:gbdetailinventory:list')")
    @GetMapping("/detailPriceList")
    public TableDataInfo detailPriceList(GbInventoryPriceVo vo) {
        if (StrUtil.isNotBlank(vo.getStartTime())) {
            vo.setStartTime(vo.getStartTime() + "-01");
        }
        if (StrUtil.isNotBlank(vo.getEndTime())) {
            int totalDaysOfMonth = DateUtil.getTotalDaysOfMonth(DateUtil.getDate(vo.getEndTime()));
            vo.setEndTime(vo.getEndTime() + "-" + totalDaysOfMonth);
        }
        startPage();
        List<InventoryPriceBo> priceBoList = gbInventoryPriceService.detailPriceList(vo);
        return getDataTable(priceBoList);
    }

    @GetMapping("/getMMADetail")
    public AjaxResult getMMADetail(GbInventoryPriceVo vo) {
        if (StrUtil.isNotBlank(vo.getStartTime())) {
            vo.setStartTime(vo.getStartTime() + "-01");
        }
        if (StrUtil.isNotBlank(vo.getEndTime())) {
            int totalDaysOfMonth = DateUtil.getTotalDaysOfMonth(DateUtil.getDate(vo.getEndTime()));
            vo.setEndTime(vo.getEndTime() + "-" + totalDaysOfMonth);
        }

        GbInventoryPriceBo priceBo = gbInventoryPriceService.getMMADetail(vo);
        return AjaxResult.success(priceBo);
    }

    // 导出清单
    @PreAuthorize("@ss.hasPermi('srlib:gbinventory:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GbInventoryPriceVo vo) {
         List<GbInventoryPriceBo> list = gbInventoryPriceService.selectConfigInventoryLibListNoPage(vo);
         ExcelUtil<GbInventoryPriceBo> util = new ExcelUtil<>(GbInventoryPriceBo.class);
         util.exportExcel(response, list, "用户数据");
    }
}
